﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets; //添加Socket类空间
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Sword.WorkBench
{

    public class Server
    {

        // Buffer for reading data 
        static Byte[] bytes = new Byte[256];
        static String data = null;

        static TcpListener server = null;

        public static int type = 1;
        private static void TaskFuncJob()
        {
            while(true){
                Thread.Sleep(1000);
                DateTime t = DateTime.Now;
                Console.WriteLine("time:"+t);
            }
        }
        private static void TaskFunc()
        {

            try
            {

                // Enter the listening loop. 
                while (true)
                {
                    Console.Write("Waiting for a connection... ");


                    // Perform a blocking call to accept requests. 
                    // You could also user server.AcceptSocket() here. 
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");


                    data = null;


                    // Get a stream object for reading and writing 
                    NetworkStream stream = client.GetStream();
                    stream.ReadTimeout = 1000;


                    int i;


                    try
                    {
                        StringBuilder sb = new StringBuilder();
                        // Loop to receive all the data sent by the client. 
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            // Translate data bytes to a ASCII string. 
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            sb.Append(data);
                            string s = sb.ToString();
                            if (s.EndsWith("\r\n\r\n"))
                            {
                                if (s.IndexOf("type1") >= 0)
                                {
                                    type = 1;
                                    Console.WriteLine("type1");
                                }
                                if (s.IndexOf("type2") >= 0)
                                {
                                    type = 2;
                                    Console.WriteLine("type2");
                                }
                                if (s.IndexOf("type3") >= 0)
                                {
                                    type = 3;
                                    Console.WriteLine("type3");
                                }
                                if (s.IndexOf("type4") >= 0)
                                {
                                    type = 4;
                                    Console.WriteLine("type4");
                                }
                            }


                        }
                    }
                    catch (Exception ex)
                    {
                    }


                    string send = "HTTP/1.1 200 OK\r\nRfewafewa: fewfewa\r\n\r\ntype:" + type;
                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(send);
                    // Send back a response. 
                    stream.Write(msg, 0, msg.Length);

                    // Shutdown and end connection 
                    client.Close();
                    Console.WriteLine("close!");
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients. 
                server.Stop();
            }

        }

        public static void start()
        {
            // Set the TcpListener on port 13000. 
            Int32 port = 8010;


            // TcpListener server = new TcpListener(port); 
            server = new TcpListener(IPAddress.Any, port);


            // Start listening for client requests. 
            server.Start();

            Thread thread = new Thread(() => TaskFunc());
            thread.Start();

            Thread threadjob = new Thread(() => TaskFuncJob());
            threadjob.Start();


        }
    }
}
